﻿//-----------------------------------------------------------------------
// <copyright file="GuildMember.Data.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using System.Data;
using GateShift.Common.Repository;
using GateShift.Common.Repository.Interfaces.DataInterfaces;

#endregion

namespace GateShift.Services.GameData.Entities
{
    /// <summary>
    /// Guild Member Entity Data Class
    /// </summary>
    internal partial class GuildMember : IGuildMemberData
    {
        #region State Management

        /// <summary>
        /// Gets a value indicating whether or not this instance has been modified
        /// </summary>
        public bool IsDirty
        {
            get
            {
                if (IsNew)
                {
                    return true;
                }

                return EntityState == EntityState.Modified;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance is new
        /// </summary>
        public bool IsNew
        {
            get
            {
                // Before it is attached if the key is null then we are new
                if (EntityState == EntityState.Detached)
                {
                    if (EntityKey == null)
                    {
                        return true;
                    }
                }

                return EntityState == EntityState.Added;
            }
        }

        /// <summary>
        /// Gets a value indicating whether or not this instance has been deleted
        /// </summary>
        public bool IsDeleted
        {
            get
            {
                return EntityState == EntityState.Deleted;
            }
        }

        #endregion

        #region Identity Management

        /// <summary>
        /// Gets an Identity based reference for this item
        /// </summary>
        public Identity ID
        {
            get
            {
                return MemberID;
            }
        }

        #endregion

        #region ID Reference Properties

        /// <summary>
        /// Gets or sets the member's rank ID with the guild
        /// </summary>
        public Identity? RankID
        {
            get
            {
                return GuildRankReference.EntityKey == null ? (Identity?)null : (long)GuildRankReference.EntityKey.EntityKeyValues[0].Value;
            }

            set
            {
                if (value.HasValue)
                {
                    if (!value.Value.CanFitInInt64())
                    {
                        throw new InvalidCastException("Identity is not of type Int64/Int32/Int16");
                    }

                    GuildRankReference.EntityKey = new EntityKey("EntityDataAdapter.GuildRanks", "RankID", Convert.ToInt64(value.Value));
                }
                else
                {
                    GuildRankReference.EntityKey = null;
                }
            }
        }

        /// <summary>
        /// Gets or sets the character ID for this guild
        /// </summary>
        public Identity? CharacterID
        {
            get
            {
                return CharacterReference.EntityKey == null ? (Identity?)null : (Guid)CharacterReference.EntityKey.EntityKeyValues[0].Value;
            }

            set
            {
                if (value.HasValue)
                {
                    if (!value.Value.CanFitInGuid())
                    {
                        throw new InvalidCastException("Identity is not of type Guid");
                    }

                    CharacterReference.EntityKey = new EntityKey("EntityDataAdapter.Characters", "CharacterID", (Guid)value.Value);
                }
                else
                {
                    CharacterReference.EntityKey = null;
                }
            }
        }

        /// <summary>
        /// Gets or sets the associated member's guild ID
        /// </summary>
        public Identity? GuildID
        {
            get
            {
                return GuildReference.EntityKey == null ? (Identity?)null : (long)GuildReference.EntityKey.EntityKeyValues[0].Value;
            }

            set
            {
                if (value.HasValue)
                {
                    if (!value.Value.CanFitInInt64())
                    {
                        throw new InvalidCastException("Identity is not of type Int64/Int32/Int16");
                    }

                    GuildReference.EntityKey = new EntityKey("EntityDataAdapter.Guilds", "GuildID", Convert.ToInt64(value.Value));
                }
                else
                {
                    GuildReference.EntityKey = null;
                }
            }
        }

        #endregion
    }
}